<%@ page contentType="text/html; charset=utf-8" language="java"  %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%@include file="/static/common/header.jsp"%>
<%@include file="/static/common/easyui.jsp"%>
<script src="${ctx}/static/jquery/jquery.form.js" type="text/javascript"></script>
<link href="${ctx}/static/css/main.css" rel="stylesheet">
<script>
	var options = { 
	 	beforeSubmit: progressing,
        contentType: "application/x-www-form-urlencoded;charset=utf-8", 
		dataType:"json",  
        success: function(jsonStr){	
			 progressed();			 
			if(jsonStr.STATE=='ERROR'){
					showInfo("操作失败:"+jsonStr.INFO);
			}else{
				$('#menuEditWin').window('close');	
				reloadmenuTreeGridData();
				reloadUpMenuIdCombobox();
				showInfo();
			}
		},
		resetForm:true
    }; 
	
	function reloadmenuTreeGridData(){
		$('#menuTreeGridData').treegrid('reload');
		reloadUpMenuIdCombobox();
	}
	
	function reloadUpMenuIdCombobox(){
		$('#upMenuIdCombobox').combobox('setValue','');
		$('#upMenuIdCombobox').combobox('reload', '${ctx}/Mng/Menu/topMenuData')
	}

	function getSelectedId(){
		var row = $('#menuTreeGridData').datagrid('getSelected');
		if (row){
			return row.id;
		}else{
			return "";
		}
	}
	function toUpdateMenu(){
		var isValid = $("#menuEditForm").form('validate');
		if($("#menuLevel").val()!="1" && $('#upMenuIdCombobox').combobox('getValue')==""){
			isValid=false;
			showInfoCenter("请选择上级菜单");
		}
		if($("#id").val()!=""&&($("#id").val() == $('#upMenuIdCombobox').combobox('getValue'))){
			showInfoCenter("请重新选择上级菜单！");
			isValid=false;
		}
		if (!isValid){
			$.messager.progress('close');
		}
		if(isValid){
			$('#menuEditForm').ajaxSubmit(options);
		}else{
			$.messager.progress('close');
		}
		
	}
	function modify(OPT){
	 	$('#menuEditForm').resetForm();	
		$("#id").val("");//莫名其妙！！
		 mId=getSelectedId();
		 if(OPT=='N'){
		 	mId="";$("#mcbox").css("display","none");
		 }else if(mId==""){
		 	showInfoCenter("请选择要修改的数据！");
			return;
		 }
		 $('#menuEditWin').window('open');
		 
		 if(mId!=''){
			 $.ajax({
				type:"post",
				cache: false,
				dataType:"json",
				async:false,
				url: "${ctx}/Mng/Menu/detail",
				data: "mId="+mId,//+"&stmp="+(new Date()).getTime()
				beforeSend:function(){ progressing()}, complete:function(){progressed()},
				success:function(jsonStr){
					reloadUpMenuIdCombobox();
					if(jsonStr.STATE=='ERROR'){
						showInfo("操作失败:"+jsonStr.INFO);
					}else{
						$("#menuEditForm").form("load",jsonStr);
						levelSet();
					}
				}
			});
		}
	 }
	 function levelSet(){//菜单级别修改
	 	if($('#upMenuIdCombobox').combobox('getValue')=="1")
			$('#upMenuIdCombobox').combobox('setValue','');
	 	if($("#menuLevel").val()=="1"){
			$("#mcbox").css("display","none");
		}else{
			$("#mcbox").css("display","");
		}
	 }
	 function modifyOpt(){//内部按钮
	 	$('#operateEditForm').resetForm();
	 	$('#butEditWin').window('open');
	 	
	 }
	 function editMenuBut(mId,opt){
	 	if("VIEW"==opt){//查看内部按钮
			//$('#operateEditForm').resetForm();	
			loadMenuOptButGridData(mId);
			$("#butMenuId").val(mId);
		}
		if("DEL"==opt){
			$.messager.confirm('确认', '确认删除所选?', function(r){ if (r){
			$.ajax({
				type:"post", cache: false, dataType:"json", async:false,
				url: "${ctx}/Mng/Menu/disMenu",
				data: "id="+mId,//+"&stmp="+(new Date()).getTime()
				beforeSend:function(){ progressing()}, complete:function(){progressed()},
				success:function(jsonStr){
					if(jsonStr.STATE=='ERROR'){
						showInfo("操作失败:"+jsonStr.INFO);
					}else{
						reloadmenuTreeGridData();
						showInfo();
					}
				}
			});
		}});
		}
	 }
	 
	function loadMenuOptButGridData(id){
		initOpernGridData('${ctx}/Mng/Menu/operateGridData?menuId='+id);
	}

	 var options2 = { 
	 	beforeSubmit: progressing,dataType:"json",  
        contentType: "application/x-www-form-urlencoded;charset=utf-8", 
        success: function(jsonStr){	
			 progressed();			 
			if(jsonStr.STATE=='ERROR'){
				showInfo("操作失败:"+jsonStr.INFO);
			}else if(jsonStr.STATE=='NONE'){
				showInfoCenter("提示:"+jsonStr.INFO);
			} else{
				$('#butEditWin').window('close');	
				$('#menuOptButGridData').datagrid('reload');
				showInfo();
			}
		},
		resetForm:false
    }; 
	 function toSaveBut(){
	 	$("#butMenuId").val(getSelectedId());	
	 	$('#operateEditForm').ajaxSubmit(options2);
	 }
	 
	
	function initOpernGridData(_url){
		toggleLay('show');
		$('#menuOptButGridData').datagrid({
			url:_url,
			iconCls:'icon-bumen',
			method:'post', fit:true,
			nowrap: false,
			striped: true,
			collapsible:false, 
			idField:'id',
			columns:[[
				{field:'name',title:'名称',width:80},
				{field:'code',title:'代码',width:120},
				{field:'id',title:'操作',width:100,rowspan:2,formatter:function(value){
						return '<a href="#" style="color:blue" onClick="delBut('+value+')">[ 删除 ]</a>'; 
					}},
			]],
			rownumbers:true,
			toolbar:'#tbOpt',
			onLoadSuccess:function(){
				$('#userTable').datagrid('clearSelections'); //一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
			},
			onDblClickRow:function(rowIndex, rowData) {
				updaterow(rowData);
			}
		});
	}
	
	function delBut(optId){
		$.messager.confirm('确认', '确认删除所选?', function(r){ if (r){
				$.ajax({
					type:"post", cache: false, dataType:"json", async:false,
					url: "${ctx}/Mng/Menu/deleteMenuOperate",
					data: "optId="+optId,//+"&stmp="+(new Date()).getTime()
					beforeSend:function(){ progressing()}, complete:function(){progressed()},
					success:function(jsonStr){
						if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.MSG);
						}else{
							$('#menuOptButGridData').datagrid('reload');
							showInfo();
						}
					}
				});
		}});
	}
	
	var flag="";
	function toggleLay(opt){
		if(flag==opt)return;
		flag=opt;
		if(opt=='show'){
			$('#orgLayOut').layout('expand','east'); 
		}else{
			$('#orgLayOut').layout('collapse','east'); 
		}
	}
	
	$(function() {
		//reloadUpMenuIdCombobox();
	});
</script>
</head>
<body id="orgLayOut" class="easyui-layout">
<div data-options="region:'center',collapsed:false,border:false" class="bmargin">
  <table id="menuTreeGridData" title="系统功能" class="easyui-treegrid" 
			data-options="url: '${ctx}/Mng/Menu/treeGridData',rownumbers: true,idField: 'id',treeField: 'name',fit:true,toolbar:'#tb' ">
    <thead>
      <tr>
        <th data-options="field:'name'" width="220">Name</th>
        <th data-options="field:'code'" width="100" align="right">URL</th>
        <th data-options="field:'optBut'" width="150">操作</th>
      </tr>
    </thead>
  </table>
</div>
<div data-options="region:'east',split:false,collapsed:true,border:false" style="width:460px;" class="bmargin">
  <table id="menuOptButGridData"  title='操作按钮'></table>
  
</div>
<div id="tb" style="padding:5px;height:auto"><div style="margin-bottom:5px"> 
	<shiro:hasPermission name="MenuAdd">  </shiro:hasPermission>
  <a href="#" class="easyui-linkbutton" iconCls="icon-add" onClick="modify('N')" plain="true">新增</a>

  <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onClick="modify('E')" plain="true">修改</a>
</div></div>
<div id="tbOpt" style="padding:5px;height:auto"><div style="margin-bottom:5px"> 
  <a href="#" class="easyui-linkbutton" iconCls="icon-add" onClick="modifyOpt('N')" plain="true">新增</a>
</div></div>
<div id="menuEditWin" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-edit',title:'菜单维护	'" style="width:580px;height:320px;padding:10px;">
  <form method="post" id="menuEditForm" name="menuEditForm" action="${ctx}/Mng/Menu/updateMenu">
    <input type="hidden" id="id" name="id" value=""/>
    <table width="100%" cellspacing="0" class="listTable" align="center">
	<tr>
		<th colspan="2"><span class="alertInfo">请注意菜单层级</span></th>
	</tr>
	 <tr>
        <td width="30%">菜单级别</td>
        <td>
		<select id="menuLevel" name="menuLevel" onChange="levelSet()" style="width: 150px;">
			<option value="1" >一级菜单</option>
			<option value="2" >二级菜单</option>
		</select>
        </td>
      </tr>
	  <tr id="mcbox" style="display:none;">
        <td>上级菜单</td>
        <td>
		 <select class="easyui-combobox" data-options="valueField:'id',textField:'name',editable:false,url:'${ctx}/Mng/Menu/topMenuData',editable:false" style="width: 150px;" name="upMenuId" id="upMenuIdCombobox"></select>
        </td>
      </tr>
      <tr>
        <td>菜单名称</td>
        <td><input type="text" id="name" name="name" value="" class="easyui-validatebox" data-options="required:true"/>
        </td>
      </tr>
      <tr>
        <td>URL</td>
        <td><input type="text" id="url" name="url" value="/" class="easyui-validatebox"  data-options="required:true"/>
        </td>
      </tr>
      <tr>
        <td>图标样式</td>
        <td><input type="text" id="iconCls" name="iconCls" value=""/>
        <br/>
          <span class="blueInfo" >自定义的样式[/themes/icon.css]</span> </td>
      </tr>
      <tr>
        <td>状态</td>
        <td><select id="available" name="available">
            <option value="Y">启用</option>
            <option value="N">禁用</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>排序</td>
        <td><input type="text" id="menuSort" name="menuSort" value="100" class="easyui-validatebox" data-options="required:true,validType:'digits'"/>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
        <a href="javascript:void(0);" id="btn-back" onClick="$('#menuEditWin').window('close');" class="easyui-linkbutton" iconCls="icon-back">返回</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-save'" href="javascript:void(0);" onClick="toUpdateMenu()">保存</a>
		</td>
      </tr>
    </table>
  </form>
</div>


<div id="butEditWin" class="easyui-window" data-options="modal:true,closed:true,iconCls:'icon-edit',title:'菜单内部按钮维护'" style="width:580px;height:260px;padding:10px;">
  <form method="post" id="operateEditForm" name="operateEditForm" action="${ctx}/Mng/Menu/updateOperate">
  	<input type="hidden" id="butMenuId" name="menuId" value=""/>
    <table width="100%" cellspacing="0" class="listTable" align="center">
    <tr>
		<th colspan="2"><span class="alertInfo">注意：代码不能重复</span></th>
	</tr>
      <tr>
        <td width="30%">操作名称</td>
        <td><input type="text" id="name" name="name" value="" required="true"/>
        </td>
      </tr>
      <tr>
        <td>操作代码</td>
        <td><input type="text" id="code" name="code" value="" required="true"/>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
        <a href="javascript:void(0);" id="btn-back" onClick="$('#butEditWin').window('close');" class="easyui-linkbutton" iconCls="icon-back">返回</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-save'" href="javascript:void(0);" onClick="toSaveBut()"> 保 存 </a></td>
      </tr>
    </table>
  </form>
</div>
</body>

</html>
